package com.power.sifa.approve.service.impl;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.plf.common.utils.DateUtil;
import com.plf.core.base.model.Ha_attachment;
import com.plf.core.base.service.Ha_attachmentService;
import com.plf.core.common.service.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.power.sifa.approve.model.Apl_attachment;
import com.power.sifa.approve.dao.Apl_attachmentDao;
import com.power.sifa.approve.service.Apl_attachmentService;
import com.power.sifa.approve.model.pageInfo.Apl_attachmentPageInfo;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.*;

/**
 *  申请材料附件业务层实现
 */
@Service
public class Apl_attachmentServiceImpl extends BaseServiceImpl implements Apl_attachmentService {

    @Resource
    private Apl_attachmentDao dao;
    @Resource
    private Ha_attachmentService ha_attachmentService;

    @Override
    public Apl_attachment getById(Serializable id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(Apl_attachment apl_attachment) {
        apl_attachment.setCreatetime(new Date());
    	// save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (apl_attachment.getPrimaryKey() != null) {
        	 Apl_attachment apl_attachmentfDb = dao.findById(apl_attachment.getPrimaryKey()).orElse(null);
             if(apl_attachmentfDb != null) {
                BeanUtils.copyProperties(apl_attachment, apl_attachmentfDb);
                dao.save(apl_attachmentfDb);
                return;
             }
        }
        dao.save(apl_attachment);
    }

    @Override
    public void removeById(Serializable id) {
        dao.deleteById(id);
    }

    @Override
    public Page<Apl_attachment> findAll(Apl_attachmentPageInfo pageInfo) {
        //排序等定义
        Sort sort;
        if("asc".equals(pageInfo.getOrder())) {
            sort = Sort.by(Sort.Direction.ASC, pageInfo.getSort());
        }else {
            sort = Sort.by(Sort.Direction.DESC, pageInfo.getSort());
        }
        //分页的定义
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber()<=1 ? 0 : pageInfo.getPageNumber()-1, pageInfo.getPageSize(), sort);
        //查询条件
        Specification spec = this.createSpecification(pageInfo);
        return dao.findAll(spec, pageable);
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<Integer> ids) {
        dao.removeByIds(ids);
    }

    @Override
	public List<Apl_attachment> findAll() {
		return dao.findAll();
	}

	@Override
	public void saveAll(List<Apl_attachment> list) {
		dao.saveAll(list);
	}

    @Override
    public Map queryList(String applyid) {
        List<Apl_attachment> list = dao.queryList(applyid);
        if(CollectionUtils.isEmpty(list)) {
            return null;
        }
        List<String> attachmentIds = new ArrayList<>();
        for(Apl_attachment apl_attachment : list) {
            attachmentIds.add(apl_attachment.getAttachmentid());
        }
        Map<String, Ha_attachment> ha_attachmentMap = ha_attachmentService.getHa_attachmentsByIds(attachmentIds);
        if(ha_attachmentMap == null) {
            return null;
        }
        Multimap<String, Map> result = ArrayListMultimap.create();
        Map item;
        for (Apl_attachment apl_attachment : list) {
            item = new HashMap();
            item.put("id", apl_attachment.getId());
            item.put("isread", apl_attachment.getIsread());
            item.put("createtime", DateUtil.dt2s(apl_attachment.getCreatetime()));
            Ha_attachment ha_attachment = ha_attachmentMap.get(apl_attachment.getAttachmentid());
            if(ha_attachment != null) {
                item.put("attachmentid", ha_attachment.getId());
                item.put("name", ha_attachment.getFilename());
                item.put("url", ha_attachment.getPath());
            }
            result.put(apl_attachment.getMatter_attachid(), item);
        }
        return result.asMap();
    }

    @Override
    public List<Map> getListByApplyid(String applyid) {
        return dao.getListByApplyid(applyid);
    }

    @Override
    public List<String> getMatterAttachid(String applyid) {
        return dao.getMatterAttachid(applyid);
    }


}
